package com.atkid.boot.controller;


import cn.yueshutong.springbootstartercurrentlimiting.annotation.CurrentLimiter;
import com.atkid.boot.entity.Club;
import com.atkid.boot.result.Result;
import com.atkid.boot.service.ClubService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 * @author kid
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/boot/club")
public class ClubController {
	@Autowired
	ClubService clubService;

	@GetMapping("/type/{typeName}/{current}/{limit}")
	public Result getAllClubByType(@PathVariable String typeName, @PathVariable Long current,@PathVariable Long limit) {
		QueryWrapper<Club> wrapper = new QueryWrapper<>();
		wrapper.eq("type",typeName);
		Page<Club> page = new Page<>(current, limit);
		clubService.page(page,wrapper);
		return Result.ok().data("club",page);
	}

	@GetMapping("/type/{typeName}")
	@CurrentLimiter(QPS = 50)
	public Result getClubByType(@PathVariable String typeName) {
		QueryWrapper<Club> wrapper = new QueryWrapper<>();
		wrapper.eq("type",typeName);
		return Result.ok().data("club",clubService.list(wrapper));
	}

	@PostMapping
	public Result saveClub(@RequestParam String name) {
		Club club = new Club();
		club.setName(name);
		clubService.save(club);
		return Result.ok().message("成功保存社团");
	}

	@PutMapping
	public Result updateClub(@RequestParam Integer id,@RequestParam String name) {
		Club club = clubService.getById(id);
		if (club == null) {
			return Result.error().message("未找到指定社团");
		}
		club.setName(name);
		clubService.updateById(club);
		return Result.ok().message("修改社团名称成功");
	}
}

